import axios from 'axios';
import QS from 'qs';

const isPrd = process.env.NODE_ENV_MODE == 'prod';
export const basicUrl = isPrd ? 'https://www.production.com' : 'http://www.development.com'

const service = axios.create({
  baseURL: basicUrl
})

// 请求拦截器
service.interceptors.request.use(config => { 
  const token = window.localStorage.getItem('userToken') || window.sessionStorage.getItem('userToken');
  //在每次的请求中添加token
  config.data = Object.assign({}, config.data, {
    'Content-Type': 'application/json;charset=utf-8',
    token: token,
  })
  //序列化请求参数
  config.data = QS.stringify(config.data)
  return config
}, error => { 
    return error;
})

// 响应拦截器
service.interceptors.response.use(response => {
  if (response.data.code) {
    switch (response.data.code) {
      case 200:
        return response.data;
      case 401:
        //未登录处理方法
        break;
      case 403:
        //token过期处理方法
        break;
      case 500:
      default:
    }
  } else { 
    return response;
  }
})
export default service